$(document).ready(function(){
	$('pageSelect').change(function(){
		$(this).closest('form').submit();
	});
	var page = $(this).data('page');
	var segment =$(this).data('segment');
	$(document).on('click','#btnedit',function(e){
		e.preventDefault();
		var mssv = $(this).data('mssv');
		var param = {'mssv':mssv};
		var btn = $(this);
		var url ="Suasv.php";
		var img = "<img src='images/ajax_loading.gif' />"
			$.ajax({
				url : url,
				data: param,
				type: "POST",
				dataType: "HTML",
				error: function(){},
				beforeSend: function(){
					$(btn).hide();
					$(btn).after(img);
				},
				complete:function(){
					$(btn).show();
					$(btn).next().remove();
				},
				success: function(data){
					$('#divDialogEdit').find('p').html(data);
					$('#divDialogEdit').dialog('open');
				}
			});
	});
	$('#divDialogEdit').dialog({
		resizable: false,
		height :720,
		width : 560,
		modal:true,
		autoOpen: false,
		title:"Sửa sinh viên",
		 show: {
		        effect: "Slide",
		        duration: 1000
		      },
		      hide: {
		        effect: "Slide",
		        duration: 1000
		      },
		buttons:[
		         {
		         html: "<span class='glyphicon glyphicon-floppy-disk'>Lưu </span>",
		         click: function(){
		        	 save(this);
		         }
		         },
		         {
		        	 html: "<span class='glyphicon glyphicon-remove'>Hủy </span>",
		        	 click: function(){
		        		 $(this).dialog("close");
		        	 }
		         },
		         ]
	});
	function save(btn){
		var malop=$('#MaLop').val();
		var param=$('#frmSVEdit').serialize();
		var edit=true;
		param +=' &malop='+malop+ ' &btnedit='+edit;
		var url ="Suasv.php";
		var img = "<img src='images/ajax_loading.gif' />"
			$.ajax({
				url : url,
				data: param,
				type: "POST",
				dataType: "HTML",
				error: function(){},
				beforeSend: function(){
					$(btn).hide();
					$(btn).after(img);
				},
				complete:function(){
					$(btn).show();
					$(btn).next().remove();
				},
				success: function(data){
					if(data == "OK"){
						$('#divDialogEdit').dialog("close");
						var page = $(this).data('page');
						var malop =$('#MaLop').val();
						reload(page,malop,segment);
						
					}else{
						$('#divDialogEdit').find('p').html(data);
					}
				}
			});

	}
	function reload(page,malop,segment){
		var pagam ={'malop':malop,'page':page,'segment':segment};
		var url ="timlop.php";
		var img = "<img src='images/ajax_loading.gif' />"
			$.ajax({
				url : url,
				data: pagam,
				type: "POST",
				dataType: "HTML",
				error: function(){},
				beforeSend: function(){
				
				},
				complete :function(){
					
				},
				success: function(data){
					if(data){
						$('#danhsach').html(data);
					}else{
						$('#danhsach').html("<div class='alert alert-warning'>Không tìm thấy sinh viên </div>");
					}
				}
			});

	}
	});